package com.zzy.springbootdemo.design_05_singleton;

import java.util.concurrent.atomic.AtomicReference;

/**
 * CAS
 */
public class Singleton_06 {

    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<>();

    private Singleton_06(){
    }

    public static Singleton_06 getInstance(){
        for (;;){
            Singleton_06 instance = INSTANCE.get();
            if(instance != null){
                return instance;
            }
            INSTANCE.compareAndSet(null,new Singleton_06());
            return INSTANCE.get();
        }
    }
}
